package com.pan.alg.leecode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by panstark
 * @description
 * @notice
 * @date 2021/7/11
 */
public class MoveZeros {

    public void moveZeroes(int[] nums) {
        //1、去掉所有的0
        List<Integer> noZeroList = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]!=0){
                noZeroList.add(nums[i]);
            }
        }
        //2、对非零数进行赋值
        for (int i = 0; i < noZeroList.size(); i++) {
            nums[i]=noZeroList.get(i);
        }
        //3、对剩下的零进行赋值
        for(int i=noZeroList.size();i<nums.length;i++){
            nums[i] = 0;
        }
    }


    public void moveZeroes2(int[] nums) {
        int index = 0;
        //1、去掉所有的0
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]!=0){
                nums[index]=nums[i];
                index++;
            }
        }
        //2、对剩下的零进行赋值
        for(int i=index+1;i<nums.length;i++){
            nums[i] = 0;
        }

    }

    public static void main(String[] args) {
        int[] nums =  new int[]{3,4,5,0,2,0,4};
        MoveZeros moveZeros = new MoveZeros();
        moveZeros.moveZeroes2(nums);
    }
}
